package com.unitd.modules.comm.web;

import com.unitd.frame.comm.utils.BeanUtils;
import com.unitd.frame.springweb.controller.SuperController;
import com.unitd.frame.springweb.editor.CustomDateEditor;
import com.unitd.frame.springweb.editor.StringEscapeEditor;
import com.unitd.frame.sso.common.helper.SSOHelper;
import com.unitd.frame.sso.common.token.SSOToken;
import com.unitd.frame.sso.exception.SsoException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.util.Date;

/**
 * @desc 自定义的SpringMVC的基础控制器类, 一般情况下自定义Controller类都继承自此类
 * @filename BaseController.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/4
 */
public class BaseController extends SuperController implements HandlerInterceptor {
//
//	@Autowired
//	protected ISysUserService sysUserService;
//	@Autowired
//	private ISysMenuService sysMenuService;
//	@Autowired
//	private ISysRoleService sysRoleService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
		// 方法调用后调用该方法，返回数据给请求页
		if (isLegalView(modelAndView)) {
			// 获取当前请求Token
			SSOToken token = SSOHelper.getToken(request);
			// 判断是否登录,如果未登录则直接重定向到登录页,否则获取token中用户ID并转到首页
			if(!BeanUtils.isNull(token)) {
				modelAndView.addObject("userId", getCurrentUserId());
//				modelAndView.addObject("currentUser", sysUserService.selectByPrimaryKey(getCurrentUserId()));
//				modelAndView.addObject("roleNames", sysRoleService.findRoleNameByUserId(getCurrentUserId()));
//				modelAndView.addObject("menuList", sysMenuService.findTreeListByUserId(getCurrentUserId()));
			}
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
	}

	/**
	 * @desc 获取当前登录用户的ID, 实际上为用户登录时, 将用户ID设置为SSOToken的ID, 然后再此取出
	 * @return 当前登录用户ID
	 */
	protected String getCurrentUserId() {
		return this.getSSOToken().getUid();
	}

	/**
	 * @desc 获取当前http请求中的token对象
	 * @return 请求中的token对象
	 */
	protected SSOToken getSSOToken() {
		SSOToken ssoToken = SSOHelper.attrToken(this.request);
		if (ssoToken == null) {
			throw new SsoException("用户不存在,请重新登录!");
		} else {
			return ssoToken;
		}
	}
//
//	/**
//	 * @desc 转换为 bootstrap-table 需要的分页格式数据(通过controller的response返回json)
//	 * @param pageData 分页对象对象
//	 * @return String
//	 */
//	protected Map<String, Object> restPageData(PageInfo<?> pageData) {
//		Map<String, Object> dataMap = new HashMap<>(2);
//		dataMap.put("total", pageData.getTotal());
//		dataMap.put("rows", pageData.getList());
//		return dataMap;
//	}
}